﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace VFTray
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			if (MySafe.MySingleInstance.RunningInstance())
			{
				MessageBox.Show("Already Runing");
				return;
			}

			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			/*******************全局变量初始化************/
			try
			{
				InitGlobalParams();                             //初始化配置
				if (GlobalParams.CD.Trayes.Count != GlobalParams.CR.Steps.Count)
				{
					MessageBox.Show("震动盘配置数据与配方不一致", "InitGlobalParams");
					return;
				}
			}
			catch (Exception ex)
			{
				LogUtility.LOG.Error("InitGlobalParams", ex);
				MessageBox.Show(ex.Message, "InitGlobalParams");
				return;
			}
			
			Mi.ProcessVerify _pv = new Mi.ProcessVerify();


			if (LLCVApi.CVAPI.Initialize())//加载cv配置文件
			{
				int count = LLCVApi.CVAPI.InitCount(GlobalParams.CD.Trayes.Count);
				if(count== GlobalParams.CD.Trayes.Count)
				{
					GlobalParams.cves = new List<LLCVApi.LLCV>();
					for (int i = 0; i < count; i++)
					{
						GlobalParams.cves.Add(new LLCVApi.LLCV(i));
					}
				}
				else
				{
					string txt = "NO AUTH Required(" + GlobalParams.CD.Trayes.Count.ToString() + "), Current(" + count.ToString() + ")";
					LogUtility.LOG.Error(txt);
					LogUtility.LOG.Debug("App Exit");
					MessageBox.Show(txt, "Error");
					return;
				}
			}
			else
			{
				LogUtility.LOG.Error("NO LLCV");
				LogUtility.LOG.Debug("App Exit");
				MessageBox.Show("NO LLCV", "Error");
				return;
			}
			try
			{
				var cs = new ComServer();
				GlobalParams.CS = cs;
				cs.Init();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message,"ComServer");
			}

			try
			{
				var MF = new MainForm();
				Application.Run(MF);
				LogUtility.LOG.Debug("MainForm Exit");
#if DEBUG
				GlobalParams.CDF.Save();
				GlobalParams.CRF.Save();
				LogUtility.LOG.Debug("CD Saved");
#endif
			}
			catch (Exception ex)
			{
				LogUtility.LOG.Error("Application.Run", ex);
				MessageBox.Show(ex.Message, "RUNERROR");
			}
			finally
			{
				GlobalParams.CS.Close();
				LLCVApi.CVAPI.Uninitialize();
				LogUtility.LOG.Info("Exit App");
			}
		}

		private static void InitGlobalParams()
		{
			string fn = Path.Combine(Application.StartupPath, "ConfigurationDevice.xml");
			GlobalParams.CDF = ConfigUtility.ConfigurationBase<Config.ConfigurationDevice>.Load(fn);
			GlobalParams.CD = GlobalParams.CDF.CFG;

			if (GlobalParams.CD.LogOn)
			{
				var fi = new System.IO.FileInfo(Path.Combine(Application.StartupPath + "\\log4net.config"));

				LogUtility.LOG.Configuration(fi);
				LogUtility.LOG.Info("Start App");
			}

			{
				FileInfo fi = new FileInfo(GlobalParams.CD.RecipeBootPath);
				if (fi.Directory.Exists == false)
				{
					fi.Directory.Create();
				}
				string rd = Path.Combine(GlobalParams.CD.RecipeBootPath, GlobalParams.CD.RecipeName);
				if (Directory.Exists(rd) == false)
				{
					Directory.CreateDirectory(rd);
				}
				string rf = GlobalParams.CD.GetRecipeFile();

				GlobalParams.CRF = ConfigUtility.ConfigurationBase<Config.ConfigurationRecipe>.Load(rf);
				GlobalParams.CR = GlobalParams.CRF.CFG;
			}

			LogUtility.LOG.Info("InitGlobalParams OK");

		}

	}
}
